package com.smartcampus.demo.service;


import com.smartcampus.demo.domain.Humidity;
import com.smartcampus.demo.repository.HumidityDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class HumidityService {

    @Autowired
    private HumidityDao humidityDao;

    public void saveHumidity(Humidity humidity) {
        humidity.setTime(LocalDateTime.now());
        humidityDao.save(humidity);
    }

    public List<Humidity> getAllHumidity() {
        return humidityDao.findAll();
    }

    public Humidity getHumidityById(Long id) {
        return humidityDao.findById(id).orElse(null);
    }

    public List<Humidity> getLatestNHumidity(int n) {
        return humidityDao.findTopNByOrderByCreatedAtDesc(n);
    }
}
